//
//  DDTVisitorView.swift
//  weibo
//
//  Created by mac on 16/9/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

protocol DDTVisitorViewDelegate:NSObjectProtocol {
    //注册协议
    func visitorViewRegister()
    //登陆协议
    func visitorViewLogin()
    
}


class DDTVisitorView: UIView {
    //协议属性
    weak var visitorViewDelegate: DDTVisitorViewDelegate?

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        setUpUI()
    }
    
    
    
    //首页添加动画效果
    func startRotationAnimation() {
       
      let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        
        wheelImage.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    
    //设置访客的信息
    func setUpVisitorInfo(imageName:String,message:String) {
        wheelImage.image = UIImage(named: imageName)
        describeText.text = message
        describeText.sizeToFit()
        
        //隐藏房子
        houseImage.hidden = true
        self.sendSubviewToBack(coverImage)
    }

    
    //添加子控件
    private func setUpUI() {
        self.addSubview(wheelImage)
        self.addSubview(coverImage)
        self.addSubview(houseImage)
        self.addSubview(describeText)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        //添加约束
        wheelImage.translatesAutoresizingMaskIntoConstraints = false
        coverImage.translatesAutoresizingMaskIntoConstraints = false
        houseImage.translatesAutoresizingMaskIntoConstraints = false
        describeText.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        
        
        //转轮X
        self.addConstraint(NSLayoutConstraint(item: wheelImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        //转轮Y
        self.addConstraint(NSLayoutConstraint(item: wheelImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -40))
        
        //遮盖X
        self.addConstraint(NSLayoutConstraint(item: coverImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: wheelImage, attribute: .CenterX, multiplier: 1, constant: 0))
        //遮盖Y
        self.addConstraint(NSLayoutConstraint(item: coverImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: wheelImage, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //房子X
        self.addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: wheelImage, attribute: .CenterX, multiplier: 1, constant: 0))
        //房子Y
        self.addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: wheelImage, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //Label X
        self.addConstraint(NSLayoutConstraint(item: describeText, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: wheelImage, attribute: .CenterX, multiplier: 1, constant: 0))
        //Label Y
        self.addConstraint(NSLayoutConstraint(item: describeText, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: wheelImage, attribute: .Bottom, multiplier: 1, constant: 16))
        //Label width
        self.addConstraint(NSLayoutConstraint(item: describeText, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 260))
        
        //注册 X
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: describeText, attribute: .Leading, multiplier: 1, constant: 0))
        //注册 Y
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: describeText, attribute: .Bottom, multiplier: 1, constant: 16))
        //注册 width
         self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //注册 height
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //登陆 X
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: describeText, attribute: .Trailing, multiplier: 1, constant: 0))
        //登陆 Y
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: describeText, attribute: .Bottom, multiplier: 1, constant: 16))
        //登陆 width
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //登陆 height
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    //MARK:- 点击按钮,,,,注意要加@objc
    
   @objc private func loginClick() {
//        print("点击了登陆")
    visitorViewDelegate?.visitorViewLogin()
    }
    @objc private func registerClick() {
//         print("点击了注册")
        visitorViewDelegate?.visitorViewRegister()
    }
    
    
   //MARK:- 控件懒加载 
    //转轮
    private lazy var wheelImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮盖View
    private lazy var coverImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //房子
    private lazy var houseImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    private lazy var describeText: UILabel = {
       
        let label = UILabel()
        label.text = "关注一些人,看看有上面惊喜"
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(13)
        label.sizeToFit()
        return label
    }()
    
    //注册
    private lazy var registerButton:UIButton = {
        var registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //添加点击事件
        registerBtn.addTarget(self, action:"registerClick", forControlEvents:.TouchUpInside)
        return registerBtn
    }()
    
    //登陆
    private lazy var loginButton:UIButton = {
        var loginBtn = UIButton()
        loginBtn.setTitle("登陆", forState: .Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //添加点击事件
        loginBtn.addTarget(self, action:"loginClick", forControlEvents:.TouchUpInside)
        return loginBtn
    }()

}







